package com.ruoyi.zyt.controller;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.zyt.domain.Colleges;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.zyt.domain.Field;
import com.ruoyi.zyt.service.IFieldService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 专业信息Controller
 * 
 * @author zyt
 * @date 2023-11-03
 */
@RestController
@RequestMapping("/zyt/field")
public class FieldController extends BaseController
{
    @Autowired
    private IFieldService fieldService;

    @Autowired
    private ISysDictDataService dictDataService;

    /**
     * 获取所有一级学科
     * @return
     */
    @GetMapping("/upSub")
    public AjaxResult getUpSub(){
        List<Field> fields = fieldService.selectFieldList(new Field());
        List<String> collect = fields.stream().map(Field::getUpSub).distinct().collect(Collectors.toList());
        return success(collect);
    }
    /**
     * 获取一级学科对应专业类
     * @return
     */
    @GetMapping("/getType")
    public AjaxResult getType(Field field){
        String upSub = field.getUpSub();
        if(upSub.isEmpty()){
            return error("一级学科不能为空！");
        }
        List<Field> fields = fieldService.selectFieldList(new Field());
        List<String> collect = fields.stream().filter(field1 -> upSub.equals(field1.getUpSub())).map(Field::getType).distinct().collect(Collectors.toList());
        return success(collect);
    }
    /**
     * 获取专业类下对应专业
     * @return
     */
    @GetMapping("/getField")
    public AjaxResult getField(Field field){
        String type = field.getType();
        if(type.isEmpty()){
            return error("专业类不能为空！");
        }
        List<Field> fields = fieldService.selectFieldList(new Field());
        List<String> collect = fields.stream().filter(field1 -> type.equals(field1.getType())).map(Field::getName).distinct().collect(Collectors.toList());
        return success(collect);
    }
    /**
     * 查询专业信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Field field)
    {
        startPage();
        List<Field> list = fieldService.selectFieldList(field);
        return getDataTable(list);
    }

    /**
     * 导出专业信息列表
     */
    @Log(title = "专业信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Field field)
    {
        List<Field> list = fieldService.selectFieldList(field);
        ExcelUtil<Field> util = new ExcelUtil<Field>(Field.class);
        util.exportExcel(response, list, "专业信息数据");
    }

    /**
     * 获取专业信息详细信息
     */
    @GetMapping(value = "/{fieldId}")
    public AjaxResult getInfo(@PathVariable("fieldId") Long fieldId)
    {
        return success(fieldService.selectFieldByFieldId(fieldId));
    }

    /**
     * 新增专业信息
     */
    @Log(title = "专业信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Field field)
    {
        SysDictData dict = setDict(field.getName());
        dictDataService.insertDictData(dict);
        return toAjax(fieldService.insertField(field));
    }

    /**
     * 修改专业信息
     */
    @Log(title = "专业信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Field field)
    {
        Field field1 = fieldService.selectFieldByFieldId(field.getFieldId());
        SysDictData sysDictData = setDict(field1.getName());
        List<SysDictData> list = dictDataService.selectDictDataList(sysDictData);
        if(!list.isEmpty()){
            SysDictData dictData = list.get(0);
            dictData.setDictValue(field.getName());
            dictData.setDictLabel(field.getName());
            dictDataService.updateDictData(dictData);
        }
        return toAjax(fieldService.updateField(field));
    }

    /**
     * 删除专业信息
     */
    @Log(title = "专业信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{fieldIds}")
    public AjaxResult remove(@PathVariable Long[] fieldIds)
    {
        if(fieldIds.length > 0){
            Field field = fieldService.selectFieldByFieldId(fieldIds[0]);
            SysDictData sysDictData = setDict(field.getName());
            List<SysDictData> list = dictDataService.selectDictDataList(sysDictData);
            if(!list.isEmpty()){
                Long[] longs = {list.get(0).getDictCode()};
                dictDataService.deleteDictDataByIds(longs);
            }
        }
        return toAjax(fieldService.deleteFieldByFieldIds(fieldIds));
    }


    /**
     * 获取字典对象
     * @param name
     * @return
     */
    public SysDictData setDict(String name){
        SysDictData dict = new SysDictData();
        dict.setDictLabel(name);
        dict.setDictValue(name);
        dict.setDictType("zhuanye");
        dict.setListClass("success");
        dict.setStatus("0");
        dict.setDictSort(1L);
        return dict;
    }
}
